﻿#include "commanditem.h"
#include "taskitem.h"
#include <QUuid>

CommandItem::CommandItem(const QString& sName,QObject *parent) : QObject(parent){
    msName = sName;
    mdStartTime = 0.0;
    mdEndTime = 0.0;

    mdrealewend=0.0;
    mdrealewstart=0.0;
    task = dynamic_cast<TaskItem*>(parent);

   // task=NULL;
    mnWorkMode = Mode_FastPoint;
    fastmode.mdTargetPosDuration = 0.0;
    fastmode.miTargetPosWorkMode = QString();
    this->setObjectName(QString("Command:%1:%2:%3").arg(parent->objectName().section(":",-1,-1)).arg(sName).arg(QUuid::createUuid().toString().section("-",1,1)));

    mvctScripts.clear();
}

QString CommandItem::getName()
{
    return msName;
}


void CommandItem::setValue(ScriptItem::PosType type, double value)
{
    switch(type)
    {
    case ScriptItem::Time_Start:
    {
        mdStartTime = value;
        break;
    }
    case ScriptItem::Time_End:
    {
        mdEndTime = value;
        break;
    }
    case ScriptItem::Pos_EWStart:
    {
        mdEWStart = value;
        break;
    }
    case ScriptItem::Pos_EWEnd:
    {
        mdEWEnd = value;
        break;
    }
    case ScriptItem::Pos_NSStart:
    {
        mdNSStart = value;
        break;
    }
    case ScriptItem::Pos_NSEnd:
    {
        mdNSEnd = value;
        break;
    }
    case ScriptItem::Pos_realEWstart:
    {
        mdrealewstart=value;
        break;
    }
    case ScriptItem::Pos_realEWend:
    {
        mdrealewend=value;
        break;
    }
    default:
        break;
    }
}

ScriptItem *CommandItem::addScript(const QString &name)
{
    ScriptItem* pScript = new ScriptItem(name,this);
    mvctScripts.append(pScript);
    return pScript;
}

void CommandItem::removeScript(const QString &objName)
{
    foreach (ScriptItem* pScript, mvctScripts) {
        if(pScript->objectName() == objName)
        {
            mvctScripts.removeOne(pScript);
            delete pScript;
        }
        else
        {
            continue;
        }
    }
}

QVector<ScriptItem *> CommandItem::allScripts()
{
    return mvctScripts;
}

ScriptItem *CommandItem::getScript(const QString &objName)
{
    foreach (ScriptItem* pScript, mvctScripts) {
        if(pScript->objectName() == objName)
        {
            return pScript;
        }
        else
        {
            continue;
        }
    }

    return NULL;
}

double CommandItem::getValue(ScriptItem::PosType type)
{
    switch(type)
    {
    case ScriptItem::Time_Start:
        return mdStartTime;
    case ScriptItem::Time_End:
        return mdEndTime;
    case ScriptItem::Pos_EWStart:
        return mdEWStart;
    case ScriptItem::Pos_EWEnd:
        return mdEWEnd;
    case ScriptItem::Pos_NSStart:
        return mdNSStart;
    case ScriptItem::Pos_NSEnd:
        return mdNSEnd;
    case ScriptItem::Pos_realEWstart:
        return mdrealewstart;
    case ScriptItem::Pos_realEWend:
        return mdrealewend;
    default:
        break;
    }

    return 0.0;
}

void CommandItem::setWorkMode(CommandItem::CmdWorkMode value)
{
    mnWorkMode = value;
}

CommandItem::CmdWorkMode CommandItem::getWorkMode()
{
    return mnWorkMode;
}
void CommandItem::setFastPosMode(FastTargetPos fastpos)
{
    fastmode.mdTargetPosDuration=fastpos.mdTargetPosDuration;
    fastmode.miTargetPosWorkMode=fastpos.miTargetPosWorkMode;
}

CommandItem::FastTargetPos CommandItem::getFastPosMode()
{
    return fastmode;
}
